//
//  ViewController.m
//  Words
//
//  Created by honeyrr on 2017/10/12.
//  Copyright © 2017年 honeyrr. All rights reserved.
//

#import "ViewController.h"
#import "BookItemViewController.h"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, strong) UITableView *tableView;

@property(nonatomic, strong) NSArray *books;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"词库大全";
    [self.view addSubview:self.tableView];
    [self loadBooks];
}
-(void) loadBooks
{
    NSString *str = @"hello,world";
    NSLog(@"%@",[str componentsSeparatedByString:@","]);
    
    
    //找到路径
    NSString *booksPath = [[NSBundle mainBundle] pathForResource:@"grouplist.dat" ofType:nil];
    //拿到数据
    NSString *booksData = [NSString stringWithContentsOfFile:booksPath encoding:NSUTF8StringEncoding error:nil];
    //分割总数据
    NSArray *booksArr = [booksData componentsSeparatedByString:@"\n"];
    NSLog(@"bookArr:%@",booksArr);
    //遍历分割每一条数据
    NSMutableArray *marr = [NSMutableArray new];
    for (NSString *str in booksArr) {
        NSArray *strArr = [str componentsSeparatedByString:@"#"];
        NSLog(@"strArr:%@",strArr);
        /*
         {
             "file":"xxx.dat",
             "name":"四级英语"
         }
         */
        if (![str isEqualToString:@""]) {
            NSDictionary *dict = @{@"file" : [strArr firstObject],@"name":[strArr lastObject]};
            [marr addObject:dict];
        }
    }
    [marr removeObjectAtIndex:0];
    self.books = marr;
    
    [self.tableView reloadData];
}

-(NSArray *)books
{
    if (!_books) {
        _books = [NSArray array];
    }
    return _books;
}

-(UITableView *)tableView
{
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.rowHeight = 48;
        _tableView = tableView;
    }
    return _tableView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.books.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"myCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.textLabel.text = self.books[indexPath.row][@"name"];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BookItemViewController *bookItemVC = [BookItemViewController new];
    bookItemVC.bookItem = self.books[indexPath.row][@"file"];
    bookItemVC.navigationItem.title = self.books[indexPath.row][@"name"];
    [self.navigationController pushViewController:bookItemVC animated:YES];
}
@end
